//
//  GenerateDate.m
//  E_Airplane
//
//  Created by iOS on 2017/12/26.
//  Copyright © 2017年 cj. All rights reserved.
//

#import "GenerateDate.h"

@interface GenerateDate(){
//    NSInteger fromDay;
//    NSInteger toDay;
    
    NSInteger year;
    NSInteger month;
    NSInteger day;
    
    NSInteger fromYear;
    NSInteger fromMonth;
    NSInteger fromday;
    
    NSInteger toYear;
    NSInteger toMonth;
    NSInteger today;
    
    NSInteger currentWeek;
}

@end

@implementation GenerateDate

+(instancetype)shareInstance{
    static GenerateDate *generateDate = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        generateDate = [[GenerateDate alloc] init];
    });
    return generateDate;
}

-(NSDictionary *)getFromToDateTime{
    // 获取代表公历的NSCalendar对象
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate *dt = [NSDate date];
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear;
    // 获取不同时间字段的信息
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:dt];
    
    NSInteger weekday = [comps weekday] - 1; // 星期几（注意，周日是“1”，周一是“2”。。。。）
    
    currentWeek = [comps weekOfYear]; // 今年的第几周
    
    year = comps.year;
    month = comps.month;
    day = comps.day;
    
    fromYear = comps.year;
    fromMonth = comps.month;
    fromday = comps.day;
    
    toYear = comps.year;
    toMonth = comps.month;
    today = comps.day;
    
    if (weekday == 0) {
        fromday = day - 6;
        today = day;
    }else if (weekday == 1){
        fromday = day;
        today = day + 6;
    }else if (weekday == 2){
        fromday = day - 1;
        today = day + 5;
    }else if (weekday == 3){
        fromday = day - 2;
        today = day + 4;
    }else if (weekday == 4){
        fromday = day - 3;
        today = day + 3;
    }else if (weekday == 5){
        fromday = day - 4;
        today = day + 2;
    }else if (weekday == 6){
        fromday = day - 5;
        today = day + 1;
    }
    
    NSString *toTempTime = [NSString stringWithFormat:@"%ld.%ld", year, month];
    
    NSInteger toCurMonthSunDays = [self currentMonthSumDays:toTempTime];
    
    if (today > toCurMonthSunDays) {
        toMonth++;
        NSInteger tempToDay = today - toCurMonthSunDays;
        today = 0;
        today += tempToDay;
        if (toMonth > 12) {
            toYear ++;
            toMonth = 1;
        }
    }
    
    NSString *fromTime = [NSString stringWithFormat:@"%ld.%ld.%ld",year, month, fromday];
    NSString *toTime = [NSString stringWithFormat:@"%ld.%ld.%ld",toYear, toMonth, today];
    
    NSString *time = [NSString stringWithFormat:NSLocalizedString(@"fragment_achievement_check_week_zhi", @""), fromTime, toTime];
    NSString *curWeekStr = [NSString stringWithFormat:NSLocalizedString(@"fragment_achievement_check_week_format", @""), [NSString stringWithFormat:@"%ld",currentWeek]];
    NSDictionary *dict = @{@"time":time, @"curWeek":curWeekStr};
    
    return dict;
}

-(NSDictionary *)reduceDateTime:(NSInteger)week{
    
    NSString *fromTime = @"";
    NSString *toTime = @"";
    
    fromday -= week;
    today -= week;
    if (fromday <= 0) {
        fromMonth--;
        if (fromMonth <= 0) {
            fromYear --;
            fromMonth = 12;
        }
        fromTime = [NSString stringWithFormat:@"%ld.%ld",fromYear, fromMonth];
        NSInteger fromCurMonthSunDays = [self currentMonthSumDays:fromTime];
        
        NSInteger tempFromDay = fromday;
        fromday = fromCurMonthSunDays;
        fromday += tempFromDay;
        if (fromMonth <= 0) {
            fromYear --;
            fromMonth = 12;
        }
    }
    
    if (today <= 0) {
        toMonth--;
        if (toMonth <= 0) {
            toYear --;
            toMonth = 12;
        }
        toTime = [NSString stringWithFormat:@"%ld.%ld",toYear, toMonth];
        NSInteger toCurMonthSunDays = [self currentMonthSumDays:toTime];
        
        NSInteger tempToDay = today;
        today = toCurMonthSunDays;
        today += tempToDay;
        if (toMonth <= 0) {
            toYear --;
            toMonth = 12;
        }
    }
    
    fromTime = [NSString stringWithFormat:@"%ld.%ld.%ld",fromYear, fromMonth, fromday];
    toTime = [NSString stringWithFormat:@"%ld.%ld.%ld",toYear, toMonth, today];
    
    NSString *time = [NSString stringWithFormat:NSLocalizedString(@"fragment_achievement_check_week_zhi", @""), fromTime, toTime];
    currentWeek--;
    
    if (currentWeek <= 0) {
//        NSDateComponents *comps = [ToolHelper dateStrToComponent:fromTime formatter:@"yyyy.MM.dd"];
//        currentWeek = comps.weekOfYear;
        currentWeek = 52;
    }
    NSString *curWeekStr = [NSString stringWithFormat:NSLocalizedString(@"fragment_achievement_check_week_format", @""), [NSString stringWithFormat:@"%ld",currentWeek]];
    
    NSDictionary *dict = @{@"time":time, @"curWeek":curWeekStr};
    
    return dict;
}

-(NSDictionary *)addDateTime:(NSInteger)week{
    
    NSString *fromTime = @"";
    NSString *toTime = @"";
    
    fromday += week;
    today += week;
    
//    NSInteger tempFromYear = fromYear;
//    NSInteger tempFromMonth = fromMonth;
//
//    NSInteger tempToYear = toYear;
//    NSInteger tempToMonth = toMonth;
//
//    if (fromday > 30 || fromday > 31) {
//        tempFromMonth ++;
//        if (tempFromMonth > 12) {
//            tempFromYear ++;
//            tempFromMonth = 1;
//        }
//    }
//
//    if (today > 30 || today > 31) {
//        tempToMonth ++;
//        if (tempToMonth > 12) {
//            tempToYear ++;
//            tempToMonth = 1;
//        }
//    }
    
    fromTime = [NSString stringWithFormat:@"%ld.%ld", fromYear, fromMonth];
    toTime = [NSString stringWithFormat:@"%ld.%ld", toYear, toMonth];
    
    NSInteger fromCurMonthSunDays = [self currentMonthSumDays:fromTime];
    NSInteger toCurMonthSunDays = [self currentMonthSumDays:toTime];
    
    if (fromday > fromCurMonthSunDays) {
        fromMonth++;
        
        NSInteger tempFromDay = fromday - fromCurMonthSunDays;
        fromday = 0;
        fromday += tempFromDay;
        if (fromMonth > 12) {
            fromYear ++;
            fromMonth = 1;
        }
    }
    
    if (today > toCurMonthSunDays) {
        toMonth++;
        NSInteger tempToDay = today - toCurMonthSunDays;
        today = 0;
        today += tempToDay;
        if (toMonth > 12) {
            toYear ++;
            toMonth = 1;
        }
    }
    
    currentWeek++;
    if (currentWeek > 52) {
        currentWeek = 1;
    }
    
    fromTime = [NSString stringWithFormat:@"%ld.%ld.%ld", fromYear, fromMonth, fromday];
    toTime = [NSString stringWithFormat:@"%ld.%ld.%ld", toYear, toMonth, today];
    
    NSString *time = [NSString stringWithFormat:NSLocalizedString(@"fragment_achievement_check_week_zhi", @""), fromTime, toTime];
    NSString *curWeekStr = [NSString stringWithFormat:NSLocalizedString(@"fragment_achievement_check_week_format", @""), [NSString stringWithFormat:@"%ld",currentWeek]];
    NSDictionary *dict = @{@"time":time, @"curWeek":curWeekStr};
    
    return dict;
}

-(NSInteger)currentMonthSumDays:(NSString *)dateStr{
    
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"yyyy.MM";
    NSDate *date = [df dateFromString:dateStr];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    NSUInteger numberOfDaysInMonth = range.length;
    return numberOfDaysInMonth;
}

@end
